
class ScreenUpload extends egret.DisplayObjectContainer {
    public constructor() {
        super()
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this)
    }
    private init() {
        this.createBS()
        this.craeteBtnUP()
        this.createBtnSX()
        this.createBtnSB()
    }
    private createBS() {
        const logo = Utils.createBitmapByName('logo_png')
        this.addChild(logo)
        Utils.anchorCenter(logo)
        logo.x = this.stage.stageWidth / 2
        logo.y = 50
        const tip = Utils.createBitmapByName('tip_uplod_png')
        this.addChild(tip)
        Utils.anchorCenter(tip)
        tip.x = this.stage.stageWidth / 2
        tip.y = 160
    }
    private craeteBtnUP() {
        const btn = Utils.createBitmapByName('tip_image_png')
        this.addChild(btn)
        Utils.anchorCenter(btn)
        btn.x = this.stage.stageWidth / 2
        btn.y = 430
        console.log(wx.chooseImage)
        if (wx.chooseImage) {
            console.log(456)
            $('#miva_input_div').show()
            $('#miva_input_div').on('click', () => {
                $('.loading-mask').show().css('background-color', 'rgba(0,0,0,.5)')
                $('.loading-text').html('图片上传中...')
                wx.chooseImage({
                    count: 1, // 默认9
                    sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
                    sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
                    success: function (res) {
                        var localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
                        console.log(res)
                        wx.getLocalImgData({
                            localId: localIds[0], // 图片的localID
                            success: function (res) {
                                var localData = res.localData; // localData是图片的base64数据，可以用img标签显示
                                ParamsPool.USER_PHOTH = localData
                                $('.upload').show().attr('src', localData)
                                $('.loading-mask').hide()
                            }
                        });
                    }
                })
            })
        } else {
            console.log(123)
            $('#miva_input').show()
            $('#miva_input').on('change', evt => {
                $('.loading-mask').show().css('background-color', 'rgba(0,0,0,.5)')
                $('.loading-text').html('图片上传中...')
                const file = evt.target.files[0]
                const reader = new FileReader()
                reader.readAsDataURL(file)
                reader.onloadend = e => {
                    const miva: any = e.target
                    ParamsPool.USER_PHOTH = miva.result
                    $('.upload').show().attr('src', miva.result)
                    $('.loading-mask').hide()
                }
            })
        }

    }
    private createBtnSX() {
        const group = new egret.Sprite()
        this.addChild(group)
        const btn_man = Utils.createBitmapByName('man_acr_png')
        group.addChild(btn_man)
        const btn_woman = Utils.createBitmapByName('woman_png')
        group.addChild(btn_woman)
        btn_woman.x = btn_man.x + btn_man.width + 50

        Utils.anchorCenter(group)
        group.x = this.stage.stageWidth / 2
        group.y = 710

        const man = RES.getRes('man_png')
        const woman = RES.getRes('woman_png')
        const manACT = RES.getRes('man_acr_png')
        const womanACT = RES.getRes('woman_act_png')
        console.log(man)
        btn_man.touchEnabled = true
        btn_man.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            btn_man.texture = manACT
            btn_woman.texture = woman
            ParamsPool.USER_SEX = Math.floor(Math.random() * 2 + 1)
            console.log(ParamsPool.USER_SEX)
        }, this)

        btn_woman.touchEnabled = true
        btn_woman.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            btn_man.texture = man
            btn_woman.texture = womanACT
            ParamsPool.USER_SEX = Math.floor(Math.random() * 2 + 1) + 2
            console.log(ParamsPool.USER_SEX)
        }, this)
    }
    private createBtnSB() {
        const btn = Utils.createBitmapByName('submit_png')
        this.addChild(btn)
        btn.y = 835
        Utils.anchorCenter(btn)
        btn.x = this.stage.stageWidth / 2
        btn.touchEnabled = true
        btn.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            $('.loading-mask').show().css('background-color', 'rgba(0,0,0,.5)')
            $('.loading-text').html('人脸识别验证中...')
            // 在这里进行人脸识别
            $.post('https://api-cn.faceplusplus.com/facepp/v3/detect', {
                api_key: 't2ACALhMviNagZtF6QqPAuDQtv8Nb5XU',
                api_secret: 'zzQSCGtK-2zHTg58DB4QcWwFBKMNn9sw',
                image_base64: ParamsPool.USER_PHOTH,
            }).then(res => {
                console.log(res)
                if (res.face_num < 1) {
                    $('.loading-mask').hide()
                    alert('未识别到人脸')
                    return
                }
                const input = $('#miva_input')
                input.hide()
                $('.upload').hide()
                $('.loading-text').html('人脸合成中...')
                this.dispatchEvent(new egret.Event('next'))
            }).catch(err => {
                console.log(err)
                $('.loading-mask').hide()
                alert('人脸识别失败')
            })

        }, this)
    }
}